{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Remove Letter To Equalize Frequency"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #hash-table #string #counting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #哈希表 #字符串 #计数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: equalFrequency"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #删除字符使频率相同"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个下标从 <strong>0</strong>&nbsp;开始的字符串&nbsp;<code>word</code>&nbsp;，字符串只包含小写英文字母。你需要选择 <strong>一个</strong>&nbsp;下标并 <strong>删除</strong>&nbsp;下标处的字符，使得 <code>word</code>&nbsp;中剩余每个字母出现 <strong>频率</strong>&nbsp;相同。</p>\n",
    "\n",
    "<p>如果删除一个字母后，<code>word</code>&nbsp;中剩余所有字母的出现频率都相同，那么返回 <code>true</code>&nbsp;，否则返回 <code>false</code>&nbsp;。</p>\n",
    "\n",
    "<p><strong>注意：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li>字母&nbsp;<code>x</code>&nbsp;的 <strong>频率</strong><strong>&nbsp;</strong>是这个字母在字符串中出现的次数。</li>\n",
    "\t<li>你 <strong>必须</strong>&nbsp;恰好删除一个字母，不能一个字母都不删除。</li>\n",
    "</ul>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>word = \"abcc\"\n",
    "<b>输出：</b>true\n",
    "<b>解释：</b>选择下标 3 并删除该字母：word 变成 \"abc\" 且每个字母出现频率都为 1 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>word = \"aazz\"\n",
    "<b>输出：</b>false\n",
    "<b>解释：</b>我们必须删除一个字母，所以要么 \"a\" 的频率变为 1 且 \"z\" 的频率为 2 ，要么两个字母频率反过来。所以不可能让剩余所有字母出现频率相同。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>2 &lt;= word.length &lt;= 100</code></li>\n",
    "\t<li><code>word</code>&nbsp;只包含小写英文字母。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [remove-letter-to-equalize-frequency](https://leetcode.cn/problems/remove-letter-to-equalize-frequency/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [remove-letter-to-equalize-frequency](https://leetcode.cn/problems/remove-letter-to-equalize-frequency/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"abcc\"', '\"aazz\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        cnt = sorted(Counter(word).values())  # 出现次数从小到大排序\n",
    "        # 只有一种字符 or 去掉次数最少的 or 去掉次数最多的\n",
    "        return len(cnt) == 1 or \\\n",
    "               cnt[0] == 1 and len(set(cnt[1:])) == 1 or \\\n",
    "               cnt[-1] == cnt[-2] + 1 and len(set(cnt[:-1])) == 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        word1 = []\n",
    "        isok = []\n",
    "        for w in word:\n",
    "            word1.append(word.replace(w, '', 1))\n",
    "        for wd in word1:\n",
    "            count = []\n",
    "            for d in wd:\n",
    "                count.append(wd.count(d))\n",
    "            if len(list(set(count))) == 1:\n",
    "                isok.append('t')\n",
    "            if len(list(set(count))) != 1:\n",
    "                isok.append('f')\n",
    "        if 't' in isok:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        \n",
    "        from collections  import defaultdict\n",
    "        d = defaultdict(int)\n",
    "        for c in word:\n",
    "            d[c] += 1\n",
    "        vec = [ d[k] for k in d ]\n",
    "        if len(vec) == 1:\n",
    "            return True\n",
    "        min_v = min(vec)   \n",
    "        max_v = max(vec)\n",
    "        vec2 = [ (d[k]-min_v)  for k in d ]   \n",
    "         \n",
    "        if sum(vec2) == 1:\n",
    "            return True\n",
    "        \n",
    "        vec3 = [ (max_v-d[k])  for k in d ] \n",
    "        if min_v == 1:\n",
    "             if sum(vec2) == 0:   \n",
    "                 return True\n",
    "             if sum(vec3) == max_v-1:\n",
    "                 return True    \n",
    "        return False    \n",
    "          "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        cnt = sorted(Counter(word).values())\n",
    "        return len(cnt) == 1 or cnt[0] == 1 and len(set(cnt[1:])) == 1 or cnt[-1] == cnt[-2] + 1 and len(set(cnt[:-1])) == 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "\n",
    "        for i in range(len(word)):\n",
    "            remain = word[:i] + word[i+1:]\n",
    "            freq = Counter(remain) #将字符串转化为字典counter迭代器\n",
    "            if len(set(freq.values())) == 1:#取里面的值转化为为集合，因为集合不包含重复数据，所以剩下的所有\n",
    "            #字符的长度都是相同的，所以集合的长度应该是1\n",
    "                return True\n",
    "\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        n = len(word)\n",
    "        if n<4:\n",
    "            return True\n",
    "        tem = word\n",
    "        for i in range(n):\n",
    "            word = tem[:i]+tem[i+1:]\n",
    "            count = Counter(word)\n",
    "            num = -1\n",
    "            flag = 0\n",
    "            for i in count.keys():\n",
    "                if num==-1:\n",
    "                    num = count[i]\n",
    "                else:\n",
    "                    if count[i]!=num:\n",
    "                        flag = 1\n",
    "                        break\n",
    "            if flag==0:\n",
    "                return True        \n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        a = set(word)\n",
    "        b = []\n",
    "        for i in a:\n",
    "            c = 0\n",
    "            for j in word:\n",
    "                if i == j:\n",
    "                    c += 1\n",
    "            b.append(c)\n",
    "        b.sort()\n",
    "        if len(b) == 1:\n",
    "            return True\n",
    "        if len(b) == 2:\n",
    "            if min(b) == 1:\n",
    "                return True\n",
    "            if abs(b[0] - b[1]) == 1:\n",
    "                return True\n",
    "            else:\n",
    "                return False\n",
    "        elif b[0]==1 and max(b[1:]) == min(b[1:]):\n",
    "            return True\n",
    "        if max(b[:-1]) == min(b[:-1]) == (b[-1]-1):\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        import copy\n",
    "        dic={}\n",
    "        lis=[]\n",
    "        for i in word:\n",
    "            if i in dic:\n",
    "               dic[i]+=1\n",
    "            if i not in dic:\n",
    "               dic[i]=1\n",
    "       # return max(dic.values())\n",
    "        if max(dic.values())==1:\n",
    "           return True\n",
    "        for i in dic:\n",
    "            lis.append(dic[i])\n",
    "       # return lis\n",
    "        \n",
    "        s=[]\n",
    "        t=[]\n",
    "        for i in range(len(lis)):\n",
    "            s=copy.deepcopy(lis)\n",
    "           # return s\n",
    "            s[i]-=1\n",
    "            #return s\n",
    "            if s[i]==0:\n",
    "               s.pop(i)\n",
    "            #return min(s)\n",
    "           # return s\n",
    "            #return lis\n",
    "            if max(s)==min(s):\n",
    "               return True\n",
    "        return False\n",
    "  \n",
    "  \n",
    "  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        for i in range(len(word)):  # 枚举删除的字符\n",
    "            # Counter 计数类函数 统计出现的次数 传入['b', 'c', 'a', 'b', 'c']\n",
    "            cnt = Counter(word[:i] + word[i + 1:])  # 统计出现次数\n",
    "            # 获取valuce 转化位set 若所有出现的次数相等 则长度等于1\n",
    "            if len(set(cnt.values())) == 1:  # 出现次数都一样\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        # 建立哈希表\n",
    "        cnt = [0] * 26\n",
    "        for c in word:\n",
    "            cnt[ord(c) - ord('a')] += 1\n",
    "        \n",
    "        # 枚举\n",
    "        for i, c in enumerate(cnt):\n",
    "            s = set(cnt[:i] + cnt[i + 1:])\n",
    "            if c == 0:\n",
    "                continue\n",
    "\n",
    "            # 剩下的要麽都是一个值，要麽还有0值，如\"abbcc\"\n",
    "            elif c == 1 and len(s - {0}) == 1:\n",
    "                return True\n",
    "            else:\n",
    "                # 可能只有一种字母，如\"zzzz\"\n",
    "                if s == {0}:\n",
    "                    return True\n",
    "                # 剩下的值，除去不存在的字母计数值外，必须和当前字母计数值-1相等\n",
    "                elif s - {0} == {c - 1}:\n",
    "                    return True\n",
    "\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        for i in range(len(word)):  # 枚举删除的字符\n",
    "            cnt = Counter(word[:i] + word[i + 1:])  # 统计出现次数\n",
    "            if max(cnt.values()) == min(cnt.values()):  # 出现次数都一样\n",
    "                return True\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        charcount=[0]*26\n",
    "        for c in word:\n",
    "            charcount[ord(c)-ord(\"a\")]+=1 #转数字同时记录各数字的频率\n",
    "        for i in range(26):\n",
    "            if charcount[i]==0:continue #选一个数字 （跳出本次循环）\n",
    "            charcount[i]-=1\n",
    "            frequency=set(f for f in charcount if f>0)\n",
    "            if len(frequency)==1:\n",
    "                return True #只算不同的频率\n",
    "            charcount[i]+=1\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        dic = {}\n",
    "        lst = []\n",
    "        for i in word:\n",
    "            if i in dic.keys():\n",
    "                dic[i] += 1\n",
    "            else:\n",
    "                dic[i] = 1\n",
    "        for i in dic.values():\n",
    "            lst.append(i)\n",
    "        lst.sort()\n",
    "        if len(lst) == 1:\n",
    "            return True\n",
    "        elif lst[-1]-lst[-2]==1 and lst[0]==lst[-2]:\n",
    "            return True\n",
    "        elif lst[0]==1 and lst[1]==lst[-1]:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        for i in range(len(word)):\n",
    "            cnt = Counter(word[:i] + word[i+1:])\n",
    "            if len(set(cnt.values())) == 1:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        cnt = Counter(word)\n",
    "        if len(cnt) == 1:\n",
    "            return True\n",
    "        cnt = Counter(cnt.values())\n",
    "        if len(cnt) == 2:\n",
    "            (k1, v1), (k2, v2) = cnt.items()\n",
    "            if k2 == k1 + 1 and v2 == 1:\n",
    "                return True\n",
    "            elif k1 == k2 + 1 and v1 == 1:\n",
    "                return True\n",
    "            elif k1 == 1 and v1 == 1:\n",
    "                return True\n",
    "            elif k2 == 1 and v2 == 1:\n",
    "                return True\n",
    "        elif len(cnt) == 1 and 1 in cnt.keys():\n",
    "            return True\n",
    "        return False "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        a = {}\n",
    "        z = 0\n",
    "        for i in word:\n",
    "            if a.get(i) == None:\n",
    "                a[i] = 1\n",
    "            else:\n",
    "                a[i] += 1\n",
    "        for key in a.keys():\n",
    "            temp = a[key]\n",
    "            a[key] -= 1\n",
    "            fre = set(x for x in a.values() if x > 0)\n",
    "            if len(fre) == 1:\n",
    "                return True\n",
    "            a[key] = temp    \n",
    "        return False \n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        lst = sorted(Counter(word).values())\n",
    "        return len(lst) == 1 or lst[0] == lst[-2] == lst[-1] - 1 or (lst[0] == 1 and lst[1] == lst[-1])\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        dic = Counter(word)\n",
    "        for c in word:\n",
    "            t = dic.copy()\n",
    "            t[c] -= 1\n",
    "            if t[c] == 0:\n",
    "                del(t[c])\n",
    "            if len(set(t.values())) == 1:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        for i in range(len(word)):\n",
    "            ans=Counter(word[:i]+word[i+1:])\n",
    "            if len(set(ans.values()))==1:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        w=collections.Counter(word)\n",
    "        w1=sorted(w.values(),reverse=True)\n",
    "        w2=w1[::]\n",
    "        if w1[-1]==1:\n",
    "            w1.pop()\n",
    "        x=w2[:len(w2)-1]+[w2[-1]-1]\n",
    "        y=w2[1:]+[w2[0]-1]\n",
    "        return len(set(x))==1 or len(set(y))==1 or (len(set(w1))==1 and 1 in w2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import collections\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        cnt = collections.Counter(word)\n",
    "        for c in cnt.keys():\n",
    "            cnt[c] -= 1\n",
    "            if len(set(v for v in cnt.values() if v)) == 1:\n",
    "                return True\n",
    "            cnt[c] += 1\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        temps = [0 for i in range(26)]\n",
    "\n",
    "        for c in word:\n",
    "            temps[ord(c) - 97] += 1\n",
    "        \n",
    "        cc = []\n",
    "        for temp in temps:\n",
    "            if temp != 0:\n",
    "                cc.append(temp)\n",
    "        if len(cc) == 1:\n",
    "            return True\n",
    "        cc.sort()\n",
    "        if cc[0] == cc[-1]:\n",
    "            if cc[-1] != 1:\n",
    "                return False\n",
    "            else:\n",
    "                return True\n",
    "        cc1 = [c for c in cc]\n",
    "        cc2 = [c for c in cc]\n",
    "        cc1[0] -= 1\n",
    "        if cc1[0] == 0:\n",
    "            cc1.pop(0)\n",
    "        if cc1[0] == cc1[-1]:\n",
    "            return True\n",
    "        \n",
    "        cc2[-1] -= 1\n",
    "        if cc2[-1] == 0:\n",
    "            cc2.pop(0)\n",
    "        cc2.sort()\n",
    "        if cc2[0] == cc2[-1]:\n",
    "            return True\n",
    "        \n",
    "\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        word1 = []\n",
    "        isok = []\n",
    "        for w in word:\n",
    "            word1.append(word.replace(w, '', 1))\n",
    "        for wd in word1:\n",
    "            count = []\n",
    "            for d in wd:\n",
    "                count.append(wd.count(d))\n",
    "            if len(list(set(count))) == 1:\n",
    "                isok.append('t')\n",
    "            if len(list(set(count))) != 1:\n",
    "                isok.append('f')\n",
    "        if 't' in isok:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        for c in word:\n",
    "            t = Counter(word)\n",
    "            t[c] -= 1\n",
    "            if t[c] == 0:\n",
    "                del(t[c])\n",
    "            if len(set(t.values())) == 1:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        word_count={}\n",
    "        word1=[]\n",
    "        for j in word:\n",
    "            word_count[j]=word.count(j)\n",
    "        for i in word_count.items():\n",
    "            word1.append(i[1])\n",
    "        word_add=max(word1)-1\n",
    "        word_odd=min(word1)-1\n",
    "        c=word1.copy()\n",
    "        word1.remove(max(word1))\n",
    "        word1.append(word_add)\n",
    "        a=word1\n",
    "        c.remove(min(c))\n",
    "        c.append(word_odd)\n",
    "        b=c\n",
    "        word_a=set(a)\n",
    "        word_b=set(b)\n",
    "        if 0 in word_a:\n",
    "            word_a.remove(0)\n",
    "        if 0 in word_b:\n",
    "            word_b.remove(0)\n",
    "        if len(word_a)==1 or len(word_b)==1:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "        \n",
    "\n",
    "\n",
    "        \n",
    "        \n",
    "\n",
    "            \n",
    "                \n",
    "\n",
    "                \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        cnt = sorted(Counter(word).values())  # 出现次数从小到大排序\n",
    "        # 只有一种字符 or 去掉次数最少的 or 去掉次数最多的\n",
    "        return len(cnt) == 1 or \\\n",
    "               cnt[0] == 1 and len(set(cnt[1:])) == 1 or \\\n",
    "               cnt[-1] == cnt[-2] + 1 and len(set(cnt[:-1])) == 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        cnt=sorted(Counter(word).values())\n",
    "        return len(cnt)==1 or  (cnt[0]==1 and cnt[1]==cnt[-1]) or (cnt[0]==cnt[-2]==cnt[-1]-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        for i in range(len(word)):  # 枚举删除的字符\n",
    "            cnt = Counter(word[:i] + word[i + 1:])  # 统计出现次数\n",
    "            if len(set(cnt.values())) == 1:  # 出现次数都一样\n",
    "                return True\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        d={}\n",
    "        for c in word:\n",
    "            if c not in d:\n",
    "                d[c]=1\n",
    "            else:\n",
    "                d[c] += 1\n",
    "        a= min(d.values())\n",
    "        if len( d.keys())==1:\n",
    "            return True\n",
    "        if a==1 and (len(d.values())-1)* max(d.values())== sum(d.values())-1:\n",
    "            return True\n",
    "        b = sum([ abs(i - a) for i in d.values()])\n",
    "        if b==1 or (b==0 and a==1):\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        cnt = sorted(Counter(word).values())  # 出现次数从小到大排序\n",
    "        # 只有一种字符 or 去掉次数最少的 or 去掉次数最多的\n",
    "        return len(cnt) == 1 or \\\n",
    "               cnt[0] == 1 and len(set(cnt[1:])) == 1 or \\\n",
    "               cnt[-1] == cnt[-2] + 1 and len(set(cnt[:-1])) == 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        dic = Counter(word)\n",
    "        for i in word:\n",
    "            dicTmp = dic.copy()\n",
    "            dicTmp[i] -= 1\n",
    "            if dicTmp[i] == 0:\n",
    "                del dicTmp[i]\n",
    "            if len(set(dicTmp.values())) == 1:\n",
    "                return True\n",
    "        return False\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        result = False\n",
    "        for i in range(0,len(word)):\n",
    "            new_word = word[0:i] + word[i+1:len(word)]\n",
    "            tongji = {}\n",
    "            for j in new_word:\n",
    "                if j in tongji:\n",
    "                    tongji[j] = tongji[j] + 1\n",
    "                else:\n",
    "                    tongji[j] = 1\n",
    "            l = list(tongji.values())\n",
    "            l.sort()\n",
    "            tongji.clear()\n",
    "            if l.count(l[-1]) == len(l):\n",
    "                result = True\n",
    "                break\n",
    "            \n",
    "            else:\n",
    "                continue\n",
    "        return result\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        counter = Counter(word)\n",
    "       \n",
    "        if len(counter) == 1:\n",
    "            return True\n",
    "            \n",
    "        l = sorted(counter.values())\n",
    "        if (l[0] == l[-2] == l[-1] -1) or (l[0] == 1 and l[1] == l[-1]):\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        cnt = sorted(Counter(word).values())  # 出现次数从小到大排序\n",
    "        # 只有一种字符 or 去掉次数最少的 or 去掉次数最多的\n",
    "        return len(cnt) == 1 or \\\n",
    "               cnt[0] == 1 and len(set(cnt[1:])) == 1 or \\\n",
    "               cnt[-1] == cnt[-2] + 1 and len(set(cnt[:-1])) == 1\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        for i in range(len(word)):  # 枚举删除的字符\n",
    "            cnt = Counter(word[:i] + word[i + 1:])  # 统计出现次数\n",
    "            if len(set(cnt.values())) == 1:  # 出现次数都一样\n",
    "                return True\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        lst = sorted(Counter(word).values())\n",
    "        return len(lst) == 1 or lst[0] == lst[-2] == lst[-1] - 1 or (lst[0] == 1 and lst[1] == lst[-1])\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        cnt = Counter(word)\n",
    "        a = sorted(cnt.values())\n",
    "        return len(a) == 1 or (a[0] == 1 and len(set(a[1:])) == 1) or (a[-1] == a[-2]+1 and len(set(a[:-1])) == 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        dic = Counter(word)\n",
    "        lst = list(dic.values())\n",
    "        for i, v in enumerate(lst):\n",
    "            if v - 1 > 0:\n",
    "                temp = lst[:i] + [v - 1] + lst[i + 1:]\n",
    "            else:\n",
    "                temp = lst[:i] + lst[i + 1:]\n",
    "            if len(set(temp)) == 1:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        counter = defaultdict(int)\n",
    "        for w in word:\n",
    "            counter[w] += 1\n",
    "        for w in word:\n",
    "            counter[w] -= 1\n",
    "            cnt = counter[w]\n",
    "            if cnt == 0:\n",
    "                counter.pop(w)\n",
    "                if len(set(counter.values())) == 1:\n",
    "                    return True\n",
    "                counter[w] = 0\n",
    "            if all(cnt == c for c in counter.values()):\n",
    "                return True\n",
    "            counter[w] += 1\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "def helper_func(word: str) -> bool:\n",
    "    letter_times = dict()\n",
    "\n",
    "    for i in range(len(word)):\n",
    "        cur_letter = word[i]\n",
    "        if cur_letter not in letter_times:\n",
    "            letter_times[cur_letter] = 1\n",
    "        else:\n",
    "            letter_times[cur_letter] += 1\n",
    "    \n",
    "    for letter in letter_times.keys():\n",
    "        letter_times[letter] -= 1\n",
    "        new_val = list(filter(lambda x : x > 0, set(letter_times.values())))\n",
    "        if len(new_val) == 1:\n",
    "            return True\n",
    "        letter_times[letter] += 1\n",
    "\n",
    "    return False\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        return helper_func(word)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        charCount = [0] * 26\n",
    "        for c in word:\n",
    "            charCount[ord(c) - ord('a')] += 1\n",
    "        for i in range(26):\n",
    "            if charCount[i] == 0: continue\n",
    "            charCount[i] -= 1\n",
    "            frequency = set(f for f in charCount if f > 0)\n",
    "            if len(frequency) == 1:\n",
    "                return True\n",
    "            charCount[i] += 1\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        if len(word) < 4:\n",
    "            # 0个字符False，1~3个字符True\n",
    "            return len(word) > 0\n",
    "        \n",
    "        # 记录每个字符出现次数\n",
    "        m = {}\n",
    "        for w in word:\n",
    "            m[w] = m.get(w,0) + 1\n",
    "        #print(m)\n",
    "        if len(m) == 1:\n",
    "            # 只有一个字符\n",
    "            return True\n",
    "        \n",
    "        \n",
    "        # 记录每个频率出现次数\n",
    "        c = {}\n",
    "        for v in m.values():\n",
    "            c[v] = c.get(v,0) + 1\n",
    "        #print(c)\n",
    "        \n",
    "        if len(c) == 1:\n",
    "            # 如果只有一种频率，除非频率为1，否则False\n",
    "            return min(c) == 1\n",
    "\n",
    "        if len(c) > 2:\n",
    "            # 超过两种频率，直接False\n",
    "            return False\n",
    "\n",
    "        mmax, mmin = max(c), min(c)\n",
    "        if mmax - mmin == 1 and c[mmax] == 1:\n",
    "            # 减去较大的频率等于较小频率\n",
    "            return True\n",
    "        if mmin == 1 and c[mmin] == 1:\n",
    "            # 减去较小的频率\n",
    "            return True\n",
    "        \n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        lst = sorted(Counter(word).values())\n",
    "        return len(lst) == 1 or lst[0] == lst[-2] == lst[-1] - 1 or (lst[0] == 1 and lst[1] == lst[-1])\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        cnt=sorted(Counter(word).values())\n",
    "        return len(cnt)==1 or  (cnt[0]==1 and len(set(cnt[1:]))==1) or (cnt[0]==cnt[-2]==cnt[-1]-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        dic = {}\n",
    "        for ch in word:\n",
    "            if ch not in dic:\n",
    "                dic[ch] = 1\n",
    "            elif ch in dic:\n",
    "                dic[ch] += 1\n",
    "        dic_times = {}\n",
    "        for key in dic.keys():\n",
    "            if dic[key] not in dic_times:\n",
    "                dic_times[dic[key]] = 1\n",
    "            elif dic[key] in dic_times:\n",
    "                dic_times[dic[key]] += 1\n",
    "        if len(list(dic_times.keys())) == 1:\n",
    "            for key in dic_times.keys():\n",
    "                if key == 1:\n",
    "                    return True\n",
    "                elif key != 1:\n",
    "                    if dic_times[key] == 1:\n",
    "                        return True\n",
    "                    elif dic_times[key] != 1:\n",
    "                        return False\n",
    "        elif len(list(dic_times.keys())) == 2:\n",
    "            keys = list(dic_times.keys())\n",
    "            if dic_times[keys[0]] == 1 and keys[0] == 1:\n",
    "                return True\n",
    "            if dic_times[keys[1]] == 1 and keys[1] == 1:\n",
    "                return True\n",
    "            if dic_times[keys[0]] == 1 and keys[0] - 1 == keys[1]:\n",
    "                return True\n",
    "            if dic_times[keys[1]] == 1 and keys[1] - 1 == keys[0]:\n",
    "                return True\n",
    "            return False\n",
    "        elif len(list(dic_times.keys())) > 2:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        cnt = sorted(Counter(word).values())  # 出现次数从小到大排序\n",
    "        # 只有一种字符 or 去掉次数最少的 or 去掉次数最多的\n",
    "        return len(cnt) == 1 or \\\n",
    "               cnt[0] == 1 and len(set(cnt[1:])) == 1 or \\\n",
    "               cnt[-1] == cnt[-2] + 1 and len(set(cnt[:-1])) == 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        char_freq = dict(Counter(word))  # 统计每个字母的出现频率\n",
    "        freq_count = {}  # 记录有多少种频率以及每个频率对应了多少种字母\n",
    "        for freq in char_freq.values():\n",
    "            freq_count[freq] = freq_count.get(freq, 0) + 1\n",
    "        if len(freq_count) > 2:\n",
    "            # 出现频率多于两种 无法满足目标\n",
    "            return False\n",
    "        elif len(freq_count) == 1:\n",
    "            # 出现频率只有一种 唯一的这个频率是否为1（即`key`是否为1）或者是否只有一种字母（即value是否为1）\n",
    "            for key, value in freq_count.items():\n",
    "                if key == 1 or value == 1:\n",
    "                    return True\n",
    "            return False\n",
    "        else:\n",
    "            # 出现频率有两种 找较大频率和较小频率\n",
    "            min_freq = min(freq_count.keys())\n",
    "            max_freq = max(freq_count.keys())\n",
    "            # 要么小频率为1且只有一个字符是小频率\n",
    "            # 要么大频率等于小频率+1且只有一个字符是大频率\n",
    "            if min_freq == 1 and freq_count[min_freq] == 1 or max_freq == min_freq + 1 and freq_count[max_freq] == 1:\n",
    "                return True\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "\n",
    "        for i in range(len(word)):  # 枚举删除的字符\n",
    "\n",
    "            cnt = Counter(word[:i] + word[i + 1:])  # 统计出现次数\n",
    "\n",
    "            if len(set(cnt.values())) == 1:  # 出现次数都一样\n",
    "\n",
    "                return True\n",
    "\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        for i in range(len(word)):\n",
    "            cnt = Counter(word[:i] + word[i+1:])\n",
    "            if len(set(cnt.values())) == 1:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        for i in range(len(word)):  # 枚举删除的字符\n",
    "            cnt = Counter(word[:i] + word[i + 1:])  # 统计出现次数\n",
    "            if len(set(cnt.values())) == 1:  # 出现次数都一样\n",
    "                return True\n",
    "        return False\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        cnt = sorted(Counter(word).values())  # 出现次数从小到大排序\n",
    "        # 只有一种字符 or 去掉次数最少的 or 去掉次数最多的\n",
    "        return len(cnt) == 1 or \\\n",
    "               cnt[0] == 1 and len(set(cnt[1:])) == 1 or \\\n",
    "               cnt[-1] == cnt[-2] + 1 and len(set(cnt[:-1])) == 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        for i in range(len(word)):  # 枚举删除的字符\n",
    "            cnt = Counter(word[:i] + word[i + 1:])  # 统计出现次数\n",
    "            if len(set(cnt.values())) == 1:  # 出现次数都一样\n",
    "                return True\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        cnt=sorted(Counter(word).values())\n",
    "        return len(cnt)==1 or  (cnt[0]==1 and len(set(cnt[1:]))==1) or (cnt[0]==cnt[-2]==cnt[-1]-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        for i in range(len(word)):  # 枚举删除的字符\n",
    "            cnt = Counter(word[:i] + word[i + 1:])  # 统计出现次数\n",
    "            if len(set(cnt.values())) == 1:  # 出现次数都一样\n",
    "                return True\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        freq = Counter(Counter(word).values())\n",
    "        if len(freq) >= 3:\n",
    "            return False\n",
    "        elif len(freq) == 1:\n",
    "            return list(freq.keys())[0] == 1 or list(freq.values())[0] == 1\n",
    "        else:\n",
    "            a, b = sorted(freq.keys())\n",
    "            return (a == 1 and freq[a] == 1) or (b == a+1 and freq[b] == 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        str_list = [0]*26\n",
    "        for i in word:\n",
    "            str_list[ord(i) -ord('a')] = str_list[ord(i)-ord('a')] + 1\n",
    "        \n",
    "        mark_no = 0\n",
    "        for i in range(26):\n",
    "            if str_list[i] != 0:\n",
    "                if mark_no !=0:\n",
    "                    mark_no = min(str_list[i],mark_no)\n",
    "                else:\n",
    "                    mark_no = str_list[i]\n",
    "        check = 0\n",
    "        for i in range(26):        \n",
    "            if str_list[i] !=0:\n",
    "                if mark_no != str_list[i] and check != 1:\n",
    "                    if str_list[i] - mark_no == 1:\n",
    "                        check = 1\n",
    "                        continue\n",
    "                    else: \n",
    "                        check = 2\n",
    "                        break\n",
    "                elif mark_no != str_list[i] and check == 1:\n",
    "                        check = 2\n",
    "                        break\n",
    "        if mark_no == 1:\n",
    "            max_count = 0\n",
    "            for i in range(26):\n",
    "                max_count = max(max_count,str_list[i])\n",
    "            if max_count == 1:\n",
    "                check = 1\n",
    "            single_count =0\n",
    "            single_check = 0\n",
    "            for i in range(26):\n",
    "                if str_list[i] == 1:\n",
    "                    single_count += 1\n",
    "                elif str_list[i] >=2 and mark_no == 1:\n",
    "                    mark_no = str_list[i]\n",
    "                elif str_list[i]!=0 and str_list[i]!=mark_no:\n",
    "                    single_check=1\n",
    "                    break\n",
    "            if single_check == 0 and single_count == 1:\n",
    "                check = 1\n",
    "        only_mark = 0\n",
    "        for i in range(26):\n",
    "            if str_list[i] != 0:\n",
    "                only_mark += 1\n",
    "        if only_mark ==1:\n",
    "            check = 1\n",
    "        if check == 0 or check == 2:\n",
    "            return(bool(0))\n",
    "        else:\n",
    "            return(bool(1))\n",
    "    \n",
    "    \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        lst=[0]*27\n",
    "        for i in range(len(word)):\n",
    "            lst[ord(word[i])-97]+=1\n",
    "        while 0 in lst:\n",
    "            lst.remove(0)\n",
    "        for i in range(len(lst)):\n",
    "            lst[i]-=1\n",
    "            if lst[i]==0 and len(set(lst))==2 or len(set(lst))==1:\n",
    "                return True\n",
    "            if len(list(set(lst)))==1:\n",
    "                return True\n",
    "            lst[i]+=1\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        cnt = Counter(word)\n",
    "        for i in cnt.keys():\n",
    "            cnt[i] -= 1\n",
    "            if len(set(v for v in cnt.values() if v)) == 1:\n",
    "                return True\n",
    "            cnt[i] += 1\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        charCount = [0] * 26\n",
    "        for c in word:\n",
    "            charCount[ord(c) - ord('a')] += 1\n",
    "        freqCount = Counter(c for c in charCount if c > 0)\n",
    "        for c in charCount:\n",
    "            if c == 0: continue\n",
    "            freqCount[c] -= 1\n",
    "            if freqCount[c] == 0:\n",
    "                del freqCount[c]\n",
    "            if c - 1 > 0:\n",
    "                freqCount[c - 1] += 1\n",
    "            if len(freqCount) == 1:\n",
    "                return True\n",
    "            if c - 1 > 0:\n",
    "                freqCount[c - 1] -= 1\n",
    "            if freqCount[c - 1] == 0:\n",
    "                del freqCount[c - 1]\n",
    "            freqCount[c] += 1\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        d = {}\n",
    "        for w in word:\n",
    "            if w in d:\n",
    "                d[w] += 1\n",
    "            else:\n",
    "                d[w] = 1\n",
    "        if len(d) == 1:\n",
    "            return True\n",
    "        dd = {}\n",
    "        for k, v in d.items():\n",
    "            if v in dd:\n",
    "                dd[v].append(k)\n",
    "            else:\n",
    "                dd[v] = [k]\n",
    "        if len(dd) == 1:\n",
    "            if list(dd.keys())[0] == 1:\n",
    "                return True\n",
    "        elif len(dd) > 2:\n",
    "            return False\n",
    "        else:\n",
    "            if 1 in dd and len(dd[1]) == 1:\n",
    "                return True\n",
    "            k_list = list(dd.keys())\n",
    "            if k_list[0] - k_list[1] == 1 and len(dd[k_list[0]]) == 1:\n",
    "                return True\n",
    "            if k_list[1] - k_list[0] == 1 and len(dd[k_list[1]]) == 1:\n",
    "                return True\n",
    "        return False\n",
    "        \n",
    "        \n",
    "        \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        '''思路：\n",
    "           哈希表\n",
    "        '''\n",
    "\n",
    "        rec = {}\n",
    "        for s in word:\n",
    "            rec[s] = rec.get(s, 0) + 1\n",
    "\n",
    "        nums = [rec[i] for i in rec] # NOTE 每个值 >=1\n",
    "\n",
    "        if len(nums) == 1:\n",
    "            return True\n",
    "\n",
    "        rec = {}\n",
    "        for num in nums:\n",
    "            rec[num] = rec.get(num, 0) + 1\n",
    "\n",
    "        if len(rec) == 1 and (1 in rec):\n",
    "            return True\n",
    "\n",
    "        if len(rec) == 1 and (1 not in rec):\n",
    "            return False\n",
    "\n",
    "        if len(rec) >= 3:\n",
    "            return False\n",
    "\n",
    "        rec = list(rec.items())\n",
    "        freq1, cnt1 = rec[0]\n",
    "        freq2, cnt2 = rec[1]\n",
    "\n",
    "        if cnt1 != 1 and cnt2 != 1:\n",
    "            return False\n",
    "\n",
    "        if cnt1 == 1:\n",
    "            if freq1 == 1 or freq1 == freq2 + 1:\n",
    "                return True\n",
    "\n",
    "        if cnt2 == 1:\n",
    "            if freq2 == 1 or freq2 == freq1 + 1:\n",
    "                return True\n",
    "\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        c = Counter(word)\n",
    "        # 只有一种字符，肯定可以\n",
    "        if len(c) == 1:\n",
    "            return True\n",
    "        \n",
    "        cc = Counter(c.values())\n",
    "        # 3种频率，肯定不行\n",
    "        if len(cc) > 3:\n",
    "            return False\n",
    "        # 如果2种频率相差1，并且较大频率的数只有1个可以\n",
    "        # 如果2种频率，但其中一种为1，并且只有一个，那也可以\n",
    "        l = sorted(cc)\n",
    "        if len(cc) == 2 and (cc[1] == 1 or (l[1] - l[0] == 1 and cc[l[1]] == 1)):\n",
    "            return True\n",
    "        # 如果只有一种频率，并且那种频率为1，那也可以\n",
    "        if len(cc) == 1 and cc[1]:\n",
    "            return True\n",
    "        return False\n",
    "        \n",
    "\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        co = Counter(word)\n",
    "        # if all(co[i]==co[max(co,key=lambda x:co[x])]  for i in co):\n",
    "        #     return True\n",
    "        ans = []\n",
    "        for i in range(26):\n",
    "            c = chr(ord('a')+i)\n",
    "            if c in co:\n",
    "                co[c] -= 1\n",
    "                if all(co[i]==co[max(co,key=lambda x:co[x])] or co[i]==0  for i in co):\n",
    "                    print(co)\n",
    "                    return True\n",
    "            if c in co:\n",
    "                co[c] += 1\n",
    "        return False\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        alphabet = [0] * 26\n",
    "        for i in word:\n",
    "           letter = ord(i) - ord('a')\n",
    "           alphabet[letter] += 1\n",
    "        min = 0\n",
    "        max = 0\n",
    "        count = []\n",
    "        for i in range(len(alphabet)):\n",
    "            if max < alphabet[i]:\n",
    "                max = alphabet[i]\n",
    "            if min == 0 and alphabet[i] > 0:\n",
    "                min = alphabet[i]\n",
    "            elif min > alphabet[i] and alphabet[i] > 0:\n",
    "                min = alphabet[i]\n",
    "            if alphabet[i] > 0 and count.count(alphabet[i]) == 0:\n",
    "                count.append(alphabet[i])\n",
    "\n",
    "        print(alphabet)\n",
    "        print(count)\n",
    "        print('max')\n",
    "        print(max)\n",
    "        print('min')\n",
    "        print(min)\n",
    "\n",
    "\n",
    "        if max == 1 and min == 1:\n",
    "            return True\n",
    "        if max == min and alphabet.count(max) == 1:\n",
    "            return True\n",
    "        elif len(count) > 2:\n",
    "            return False\n",
    "        elif min == 1 and alphabet.count(min) == 1:\n",
    "            return True\n",
    "        elif max - min != 1 or alphabet.count(max) > 1:\n",
    "            return False\n",
    "        else:\n",
    "            return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        charCount = [0]*26\n",
    "        for c in word:\n",
    "            charCount[ord(c)-ord('a')] += 1\n",
    "        for i in range(26):\n",
    "            if charCount[i]==0:continue\n",
    "            charCount[i] -= 1\n",
    "            frequency = set(f for f in charCount if f>0)\n",
    "            if len(frequency)==1:\n",
    "                return True\n",
    "            charCount[i] += 1\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        count = collections.Counter(word)\n",
    "        values = list(count.values())\n",
    "\n",
    "        for k, v in enumerate(values):\n",
    "            if v == 1:\n",
    "                if self.get_result(values[:k]+values[k+1:]): return True\n",
    "            else:\n",
    "                if self.get_result(values[:k]+[v-1]+values[k+1:]): return True\n",
    "        return False\n",
    "                \n",
    "\n",
    "    def get_result(self, lst):\n",
    "        return True if len(set(lst)) == 1 else False\n",
    "            \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, s: str) -> bool:\n",
    "        cnt = Counter(s)\n",
    "        for i, c in enumerate(s):\n",
    "            cnt[c] -= 1\n",
    "            if all(i == j or cnt[k] == cnt[s[0] if i != 0 else s[1]] for j, k in enumerate(s)):\n",
    "                return True\n",
    "            cnt[c] += 1\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "        cnts = Counter(word)\n",
    "        for k, v in list(cnts.items()):\n",
    "            cnts[k] -= 1\n",
    "            if cnts[k] == 0:\n",
    "                del cnts[k]\n",
    "            if len(Counter(cnts.values())) == 1:\n",
    "                return True\n",
    "            cnts[k] += 1\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def equalFrequency(self, word: str) -> bool:\n",
    "\n",
    "        check = collections.Counter(word)\n",
    "        nums = [check[x] for x in check]\n",
    "        nums = sorted(nums)\n",
    "      \n",
    "\n",
    "\n",
    "        nums1 = nums[:]\n",
    "        nums1[0] -= 1\n",
    "        nums1 = [x for x in nums1 if x>0]\n",
    "\n",
    "        nums2 = nums[:]\n",
    "        nums2[-1] -= 1\n",
    "        nums2 = [x for x in nums2 if x>0]\n",
    "\n",
    "        return all(x==nums1[0] for x in nums1) or all(x==nums2[0] for x in nums2)\n",
    "\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
